#! /bin/sh

app="order"
yest=`date -d"-1 day" +%Y-%m-%d `
path=/home/xiaoju/logs/$app

function usageOfLogTool(){
cat << EOF >&2

Tool help you analyze log files.
usage: ./${0##*/} [ COMMAND ] [args...]
COMMAND:
showApiTime        the avg cost time of api
                   arg1: api (required)
                   arg2: startTime (default is yesterday 21:35:00)
                   arg3: endTime (default is yesterday 21:45:00)

showApiQps         the qps of api
                   arg1: api (required)
                   arg2: startTime (default is yesterday 21:35:00)
                   arg3: endTime (default is yesterday 21:45:00)

showApiCount       the call count of api
                   arg1: api (required)
                   arg2: startTime (default is yesterday 21:35:00)
                   arg3: endTime (default is yesterday 21:45:00)

showApiTop         the top 30 cost time of api
                   arg1: api (required)
                   arg2: startTime (default is yesterday 21:35:00)
                   arg3: endTime (default is yesterday 21:45:00)

showCallFrom       the call from source
                   arg1: api (required)
                   arg2: startTime (default is yesterday 21:35:00)
                   arg3: endTime (default is yesterday 21:45:00)

showThirdApi       the call third apis of api
                   arg1: api (required)
                   arg2: startTime (default is yesterday 21:35:00)
                   arg3: endTime (default is yesterday 21:45:00)

showTopCalls       the top 30 api which api call count
                   arg1: startTime (required)
                   arg2: endTime (required)

showTopTimeCost    the top 30 api which api most cost time
                   arg1: startTime (required)
                   arg2: endTime (required)

showTopConsumer    the top 30 api which consumer count
                   arg1: startTime (required)
                   arg2: endTime (required)

showSqlTopCalls    the top 30 api which sql call count
                   arg1: startTime (required)
                   arg2: endTime (required)

showMqTopCalls     the top 30 api which mq call count
                   arg1: startTime (required)
                   arg2: endTime (required)

showSystemQps      the count, cost, qps
                   arg1: startTime (required)
                   arg2: endTime (required)

showNoTimeoutError the error which not timeout
                   arg1: startTime (default is last 10 minute)
                   arg2: endTime (default is now)

showWarnCode       the warn code just for order

EXAMPLE:
./${0##*/} showApiTime publishOrder4Many 21:00:00 21:20:00
./${0##*/} showApiQps publishOrder4Many 21:00:00 21:20:00
./${0##*/} showApiTop publishOrder4Many 21:00:00 21:20:00
./${0##*/} showApiCount publishOrder4Many 21:00:00 21:20:00
./${0##*/} showCallFrom publishOrder4Many 21:00:00 21:20:00
./${0##*/} showThirdApi publishOrder4Many 21:00:00 21:20:00
./${0##*/} showTopCalls 21:00:00 21:20:00
./${0##*/} showTopTimeCost 21:00:00 21:20:00
./${0##*/} showTopConsumer 21:00:00 21:20:00
./${0##*/} showSqlTopCalls 21:00:00 21:20:00
./${0##*/} showMqTopCalls 21:00:00 21:20:00
./${0##*/} showNoTimeoutError 21:00:00 21:20:00
./${0##*/} showWarnCode

EOF
}

function showNoTimeoutError(){
st=$1
et=$2
if [ -z $1 ]; then
    st=`date -d'-10 minute' +%H:%M:%S`
fi
if [ -z $2 ]; then
    et=`date +%H:%M:%S`
fi
grep -P "^\d{4}(-\d\d){2} \d\d(:\d\d){2}" $path/error.log|grep -v 'receive response exception\|Invoke remote method timeout'|awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr >= s && timeStr <= e) print $0}'
exit 0
}

function showThirdApi(){
if [ -z $1 ]; then
    echo -n "please input the api"
    exit 2
fi
method=$1
st=$2
et=$3
if [ -z $2 ]; then
    st='21:35:00'
fi
if [ -z $3 ]; then
    et='21:45:00'
fi
flag=`grep $method $path/dubboProviderStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr >= s && timeStr <= e) print $4,$7}'|sort -r -n -k1|head -1|awk -F " " '{flag=substr($2,6); print flag}'`
grep $flag $path/dubboConsumerStat.log.$yest|awk -F "," '{ count[$2]++;cost[$2]+=$4;} END { printf("%-40s%-40s%s\n","api", "count", "time");for (i in count) {print i,count[i],cost[i]|"sort -nr -k2";}}'
exit 0
}

function showTopCalls(){
if [ -z $1 ]; then
    echo -n "please input the start time"
    exit 2
fi
if [ -z $2 ]; then
    echo -n "please input the end time"
    exit 2
fi
st=$1
et=$2
cat $path/dubboProviderStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr >= s && timeStr <= e)  {count[$2]++;cost[$2]+=$4}} END { printf("%-40s%-40s%s\n","api", "count", "time"); for(i in count)  {print i,count[i],cost[i]|"sort -nr -k2";}}' | head -30
exit 0
}

function showTopTimeCost(){
if [ -z $1 ]; then
    echo -n "please input the start time"
    exit 2
fi
if [ -z $2 ]; then
    echo -n "please input the end time"
    exit 2
fi
st=$1
et=$2
cat $path/dubboProviderStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr >= s && timeStr <= e) {print $2,$4}}'|sort -nr -k2 | head -50
exit 0
}

function showTopConsumer(){
if [ -z $1 ]; then
    echo -n "please input the start time"
    exit 2
fi
if [ -z $2 ]; then
    echo -n "please input the end time"
    exit 2
fi
st=$1
et=$2
cat $path/dubboConsumerStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr >= s && timeStr <= e)  {count[$2]++;cost[$2]+=$4}} END { printf("%-40s%-40s%s\n","api", "count", "time"); for(i in count)  {print i,count[i],cost[i]|"sort -nr -k2";}}' | head -30
exit 0
}

function showCallFrom(){
if [ -z $1 ]; then
    echo -n "please input the start time"
    exit 2
fi
if [ -z $2 ]; then
    echo -n "please input the end time"
    exit 2
fi
st=$1
et=$2
cat $path/dubboProviderStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr >= s && timeStr <= e)  {count[$6]++;cost[$6]+=$4}} END { printf("%-40s%-40s%s\n","host", "count", "time"); for(i in count)  {print i,count[i],cost[i]|"sort -nr -k2";}}' | head -100
exit 0
}

function showSqlTopCalls(){
if [ -z $1 ]; then
    echo -n "please input the start time"
    exit 2
fi
if [ -z $2 ]; then
    echo -n "please input the end time"
    exit 2
fi
st=$1
et=$2
cat $path/mysqlStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr >= s && timeStr <= e)  {count[$2]++;cost[$2]+=$4}} END { printf("%-40s%-40s%s\n","api", "count", "time"); for(i in count)  {print i,count[i],cost[i]|"sort -nr -k2";}}' | head -30
exit 0
}

function showMqTopCalls(){
if [ -z $1 ]; then
    echo -n "please input the start time"
    exit 2
fi
if [ -z $2 ]; then
    echo -n "please input the end time"
    exit 2
fi
st=$1
et=$2
cat $path/producerMessageStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr >= s && timeStr <= e)  {count[$2]++;cost[$2]+=$5}} END { printf("%-40s%-40s%s\n","api", "count", "time"); for(i in count)  {print i,count[i],cost[i]|"sort -nr -k2";}}' | head -30
exit 0
}


function showSystemQps(){
if [ -z $1 ]; then
    echo -n "please input the start time"
    exit 2
fi
if [ -z $2 ]; then
    echo -n "please input the end time"
    exit 2
fi
st=$1
et=$2
cat $path/dubboProviderStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr >= s && timeStr <= e)  {count++;cost+=$4}} END { printf("%-40s%-40s%s\n", "count", "time", "qps"); {print count, cost, cost/count}}'
exit 0
}

function showApiCount(){
if [ -z $1 ]; then
    echo -n "please input the api"
    exit 2
fi
method=$1
st=$2
et=$3
if [ -z $2 ]; then
    st='21:35:00'
fi
if [ -z $3 ]; then
    et='21:45:00'
fi
grep $method $path/dubboProviderStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr>=s && timeStr<=e ) print $0 }'|wc -l
exit 0
}

function showApiTop(){
if [ -z $1 ]; then
    echo -n "please input the api"
    exit 2
fi
method=$1
st=$2
et=$3
if [ -z $2 ]; then
    st='21:35:00'
fi
if [ -z $3 ]; then
    et='21:45:00'
fi
grep $method $path/dubboProviderStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr>=s && timeStr<=e ) {print $4,$7}}'|sort -nr -k1|head -n 30
exit 0
}

function showApiTime(){
if [ -z $1 ]; then
    echo -n "please input the api"
    exit 2
fi
method=$1
st=$2
et=$3
if [ -z $2 ]; then
    st='21:35:00'
fi
if [ -z $3 ]; then
    et='21:45:00'
fi
grep $method $path/dubboProviderStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); costStr=$4; if( timeStr>=s && timeStr<=e ) {c++;k=k+costStr} }END {print k/c}'
exit 0
}

function showWarnCode(){
grep fail $path/warn_info.log.$yest|grep -o "code:.*, msg"|awk -F ",|:" '{count[$2]++} END {for (i in count) {print i,count[i] }}'|sort -nr -k2
exit 0
}

function showApiQps(){
if [ -z $1 ]; then
    echo -n "please input the api"
    exit 2
fi
method=$1
st=$2
et=$3
if [ -z $2 ]; then
    st='21:35:00'
fi
if [ -z $3 ]; then
    et='21:45:00'
fi
count=`grep $method $path/dubboProviderStat.log.$yest | awk -F "," -v s=$st -v e=$et '{ timeStr=substr($0,12,8); if( timeStr>=s && timeStr<=e ) print $0 }'|wc -l`
time_diff=` expr $(date +%s -d"$et") - $(date +%s -d"$st")`
echo $count,$time_diff|awk -F , '{printf ("%.0f\n",$1/$2)}'
exit 0
}

if [ $# -lt 1 ]; then
    usageOfLogTool
    exit 2
fi

if [ "showApiCount" == $1 ]; then
    shift
    showApiCount "$@"
    exit 0
elif [ "showApiTime" == $1 ]; then
    shift
    showApiTime "$@"
    exit 0
elif [ "showApiQps" == $1 ]; then
    shift
    showApiQps "$@"
    exit 0
elif [ "showApiTop" == $1 ]; then
    shift
    showApiTop "$@"
    exit 0
elif [ "showTopCalls" == $1 ]; then
    shift
    showTopCalls "$@"
    exit 0
elif [ "showSqlTopCalls" == $1 ]; then
    shift
    showSqlTopCalls "$@"
    exit 0
elif [ "showMqTopCalls" == $1 ]; then
    shift
    showMqTopCalls "$@"
    exit 0
elif [ "showThirdApi" == $1 ]; then
    shift
    showThirdApi "$@"
    exit 0
elif [ "showNoTimeoutError" == $1 ]; then
    shift
    showNoTimeoutError "$@"
    exit 0
elif [ "showWarnCode" == $1 ]; then
    shift
    showWarnCode "$@"
    exit 0
elif [ "showSystemQps" == $1 ]; then
    shift
    showSystemQps "$@"
    exit 0
elif [ "showCallFrom" == $1 ]; then
    shift
    showCallFrom "$@"
    exit 0
elif [ "showTopTimeCost" == $1 ]; then
    shift
    showTopTimeCost "$@"
    exit 0
elif [ "showTopConsumer" == $1 ]; then
    shift
    showTopConsumer "$@"
    exit 0
else
    usageOfLogTool
    exit 2
fi
else
    usageOfLogTool
    exit 2
fi
